package com.cindata

import com.cindata.dictionary.*

// 应收账款表
class AccountsReceivableImport
{
    AccountsReceivableImportBatch batch

    Province province
    City city
    District district
    EstateStatusDict estateStatus
    Boolean isEstate = true
    BigDecimal unitPrice
    // 评估单价
    BigDecimal totalPrice
    // 评估总价
    Date valuationDate
    // 评估时间
    BigDecimal ltv
    // 抵押率 （%）
    BigDecimal cltv
    // 动态抵押率 （%）
    Account account
    // 直属管理机构(银行网点)
    Mortgage mortgage
    // 贷款
    MortgageStatusDict mortgageStatus

    String externalId = UUID.randomUUID().toString()
    // 质押物编号
    AccountsReceivableTypeDict type
    // 质押物类型
    String name
    // 质押物名称
    String fullName
    // 出质人名称
    ContactIdentityType identityType
    // 出质人证件类型
    String idNumber
    // 出质人证件号码
    String loanCardNumber
    // 出质人贷款卡编号
    String commonOwnerFullName
    // 其他共同所有人
    Double sharePoint
    // 所占份额

    // 交易类应收账款
    String sellerCreditRatingOrg
    // 卖方信用评级机构 1 本行评级 2 穆迪评级 3 标普评级 4 其他评级
    String sellerCreditRatingResults
    // 卖方信用评级结果
    String buyerCreditRatingOrg
    // 买方信用评级机构 1 本行评级 2 穆迪评级 3 标普评级 4其他评级
    String buyerCreditRatingResults
    // 买方信用评级结果
    Integer duration
    // 账龄
    Double amount
    // 应收账款金额
    Double invoiceOriginalAmount
    // 发票原有金额
    Double invoiceActualAmount
    // 发票实有金额
    Date endDate
    // 应收帐款到期日
    Date repaymentDate
    // 应收账款还款日期
    Boolean rightOfRecourse = false
    // 是否有追索权
    Integer gracePeriod
    // 宽限期（天）
    String buyerFullName
    // 买方名称
    String productName
    // 商品名称
    Integer productAmount
    // 商品数量
    String buyerOpeningBank
    // 买方开户行
    String buyerBankAccount
    // 买方银行帐号
    String buyerContact
    // 买方联系人
    String buyerCellphone
    // 买方联系电话
    CurrencyDict currency
    // 币种
    String invoiceNumber
    // 发票号

    // 道路收费权
    String unit
    // 收费权质押批文单位
    String number
    // 收费权质押批文号
    Double repaymentRatio
    // 收费还贷比例
    String accountNumber
    // 归集账号
    String openingBank
    // 开户行
    String cashFlowAnalysis
    // 收费权现金流分析
    String creditRatingAgency
    // 权利人信用评级机构/账户权属人信用评级机构
    String creditRatingResults
    // 权利人信用评级结果/账户权属人信用评级结果
    Double guaranteeValue
    // 担保价值

    // 出口退税专用账户
    String ownerFullName
    // 账户权属人名称
    ContactIdentityType ownerIdentityType
    // 账户权属人证件类型
    String ownerIdNumber
    // 账户权属人证件号码
    String taxRefundAccount
    // 退税专户账号
    Double untaxRefundAmount
    // 未退税额
    Date untaxRefundConfirmDate
    // 未退税额确认日期
    String untaxRefundConfirmAccount
    // 未退税额确认机构
    Date taxRefundDate
    // 退税日期

    Cashability cashability
    // 变现能力 1.通用且变现容易，2.通用但变现较不易，专用性强
    String valueVolatility
    // 价值波动性 1.价值波动大；2.价值波动中；3.有升值潜力或波动小
    String seizureOfConvenience
    // 查封便利性 1.无查封障碍的不动产，动产由我行监管；2.查封有一定障碍的不动产，动产存放地点相对固定但非我行监管；3.查封有障碍的不动产，动产存放地点难掌握和控制
    String relationOfMortgagor
    // 抵质押物与借款人相关性 1.本人 2.第三方
    String legalValidity
    // 法律有效性 1.担保合法且手续完整 2.担保不合法或手续基本无效
    Double firstEvaluatePrice
    // 初次认定价值
    Account firstEvaluateAccount
    Account latestEvaluateAccount
    Double latestEvaluatePrice
    // 最新认定价值
    Date latestEvaluateDate
    // 最新认定日期
    Double actualMortgageRate
    // 实际抵质押率 (实际抵质押率=债权本息总额÷抵押物评估价值（或质押物认定价值）×100%)
    Double mortgageRateUpperLimit
    // 抵质押率上限
    String description
    // 抵质押物说明
    String registerFullName
    // 登记人
    String registerAccount
    // 登记机构
    Date registerDate
    // 登记日期
    Date registerUpdateDate
    // 更新日期

    Date evaluateDate
    // 评估日期
    EvaluateDict evaluateType
    // 1:自动评估 2：人工评估
    ExecStatusDict evaluateStatue
    //评估状态
    String failReason
    // 评估失败原因
    String evaluateCode

    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User user
    User createBy
    User modifyBy
    static mapping = {
        autowire true
    }
    static constraints = {

        province nullable: true, blank: true, comment: '省份'
        city nullable: true, blank: true, comment: '城市'
        district nullable: true, blank: true, comment: '区县'
        estateStatus nullable: true, blank: true, comment: '贷款状态'
        isEstate nullable: true, blank: true, comment: '是否在押'
        unitPrice nullable: true, blank: true, comment: '评估单价'
        totalPrice nullable: true, blank: true, comment: '评估总价'
        valuationDate nullable: true, blank: true, comment: '评估时间'
        ltv nullable: true, blank: true, comment: '抵押率 （%）'
        cltv nullable: true, blank: true, comment: '动态抵押率 （%）'
        account nullable: true, blank: true, comment: '属管理机构(银行网点)'
        mortgage nullable: true, blank: true, comment: '贷款'
        mortgageStatus nullable: true, blank: true, comment: '贷款状态'

        externalId maxSize: 20, nullable: true, blank: true, comment: '质押物编号'
        type nullable: true, blank: true, comment: '质押物类型'
        name maxSize: 50, nullable: true, blank: true, comment: '质押物名称'
        fullName maxSize: 20, nullable: true, blank: true, comment: '出质人名称'
        identityType nullable: true, blank: true, comment: '出质人证件类型'
        idNumber maxSize: 18, nullable: true, blank: true, comment: '出质人证件号码'
        loanCardNumber maxSize: 50, nullable: true, blank: true, comment: '出质人贷款卡编号'
        commonOwnerFullName maxSize: 20, nullable: true, blank: true, comment: '其他共同所有人'
        sharePoint nullable: true, blank: true, comment: '所占份额'

        // 交易类应收账款
        sellerCreditRatingOrg maxSize: 50, nullable: true, blank: true, comment: '卖方信用评级机构'
        sellerCreditRatingResults maxSize: 200, nullable: true, blank: true, comment: '卖方信用评级结果'
        buyerCreditRatingOrg maxSize: 50, nullable: true, blank: true, comment: '买方信用评级机构'
        buyerCreditRatingResults maxSize: 200, nullable: true, blank: true, comment: '买方信用评级结果'
        duration nullable: true, blank: true, comment: '账龄'
        amount nullable: true, blank: true, comment: '应收账款金额'
        invoiceOriginalAmount nullable: true, blank: true, comment: '发票原有金额'
        invoiceActualAmount nullable: true, blank: true, comment: '发票实有金额'
        endDate nullable: true, blank: true, comment: '应收帐款到期日'
        repaymentDate nullable: true, blank: true, comment: '应收账款还款日期'
        rightOfRecourse nullable: true, blank: true, comment: '是否有追索权'
        gracePeriod nullable: true, blank: true, comment: '宽限期（天）'
        buyerFullName maxSize: 50, nullable: true, blank: true, comment: '买方名称'
        productName maxSize: 50, nullable: true, blank: true, comment: '商品名称'
        productAmount nullable: true, blank: true, comment: '商品数量'
        buyerOpeningBank maxSize: 50, nullable: true, blank: true, comment: '买方开户行'
        buyerBankAccount maxSize: 50, nullable: true, blank: true, comment: '买方银行帐号'
        buyerContact maxSize: 20, nullable: true, blank: true, comment: '买方联系人'
        buyerCellphone maxSize: 11, nullable: true, blank: true, comment: '买方联系电话'
        currency nullable: true, blank: true, comment: '币种'
        invoiceNumber maxSize: 50, nullable: true, blank: true, comment: '发票号'

        // 道路收费权
        unit maxSize: 50, nullable: true, blank: true
        number maxSize: 50, nullable: true, blank: true
        repaymentRatio nullable: true, blank: true
        accountNumber maxSize: 50, nullable: true, blank: true
        openingBank maxSize: 50, nullable: true, blank: true
        cashFlowAnalysis maxSize: 200, nullable: true, blank: true
        creditRatingAgency maxSize: 50, nullable: true, blank: true
        creditRatingResults maxSize: 200, nullable: true, blank: true
        guaranteeValue nullable: true, blank: true

        // 出口退税专用账户
        ownerFullName maxSize: 20, nullable: true, blank: true
        ownerIdentityType nullable: true, blank: true
        ownerIdNumber maxSize: 18, nullable: true, blank: true
        taxRefundAccount maxSize: 50, nullable: true, blank: true
        untaxRefundAmount nullable: true, blank: true
        untaxRefundConfirmDate nullable: true, blank: true
        untaxRefundConfirmAccount maxSize: 50, nullable: true, blank: true
        taxRefundDate nullable: true, blank: true

        cashability nullable: true, blank: true, comment: '变现能力'
        valueVolatility maxSize: 50, nullable: true, blank: true, comment: '价值波动性'
        seizureOfConvenience maxSize: 50, nullable: true, blank: true, comment: '查封便利性'
        relationOfMortgagor maxSize: 50, nullable: true, blank: true, comment: '抵质押物与借款人相关性'
        legalValidity maxSize: 50, nullable: true, blank: true, comment: '法律有效性'
        firstEvaluatePrice nullable: true, blank: true, comment: '初次认定价值'
        latestEvaluatePrice nullable: true, blank: true, comment: '最新认定价值'
        latestEvaluateDate nullable: true, blank: true, comment: '最新认定日期'
        
        firstEvaluateAccount nullable: true, blank: true
        latestEvaluateAccount nullable: true, blank: true

        actualMortgageRate nullable: true, blank: true, comment: '实际抵质押率'
        mortgageRateUpperLimit nullable: true, blank: true, comment: '抵质押率上限'
        description maxSize: 512, nullable: true, blank: true, comment: '抵质押物说明'
        registerFullName maxSize: 20, nullable: true, blank: true, comment: '登记人'
        registerAccount maxSize: 50, nullable: true, blank: true, comment: '登记机构'
        registerDate nullable: true, blank: true, comment: '登记日期'
        registerUpdateDate nullable: true, blank: true, comment: '更新日期'

        evaluateType nullable: true, blank: true
        evaluateStatue nullable: true, blank: true
        evaluateDate nullable: true, blank: true
        evaluateCode maxSize: 50, nullable: true, blank: true, comment: '评估编号'
        failReason maxSize: 200, nullable: true, blank: true, comment: '评估失败原因'

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        user nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }

    def afterInsert()
    {
        // def mortgage = Mortgage.CreateFromEstateImport(this)
    }

    def afterUpdate()
    {
        this.updateMatchNum()
    }

    // 评估成功，进入押品表
    def evaluteSuccess()
    {
        def accountsReceivable = AccountsReceivable.findByExternalId(externalId)
        if (!accountsReceivable)
        {
            accountsReceivable = new AccountsReceivable()
        }
        accountsReceivable.externalId = this.externalId
        //
        accountsReceivable.estateStatus = this.estateStatus

        accountsReceivable.unitPrice = this.unitPrice
        accountsReceivable.totalPrice = this.totalPrice
        accountsReceivable.ltv = this.ltv

        accountsReceivable.account = this.createBy.account
        accountsReceivable.isEstate = true

        accountsReceivable.save()

    }

    /*
    * 评估失败
     */

    def evaluteFailure()
    {
        def userCost = UserCost.findBySourceIdAndSourceClass(this.id, this.class.toString())
        if (userCost)
        {
            userCost.finish(this.evaluateStatue, this.failReason)
        }
    }

    // 更新匹配记录数量
    def updateMatchNum()
    {
        this.batch.updateBatchCount()
    }
}
